package com.lz.yz.base.util;



import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    private static final String defaultDatePattern = "yyyy-MM-dd";

    public static final String defaultYmdhmsPattern = "yyyy-MM-dd HH:mm:ss";

    public static final String defaultYmdhmPattern = " yyyy.MM.dd HH:mm";


    /**
     * 获得默认的 date pattern
     */
    public static String getDatePattern() {
        return defaultDatePattern;
    }

    /**
     * 返回预设Format的当前日期字符串
     */
    public static String getToday() {
        Date today = new Date();
        return format(today);
    }

    /**
     * 使用预设Format格式化Date成字符串
     */
    public static String format(Date date) {
        return date == null ? " " : format(date, getDatePattern());
    }

    /**
     * 使用参数Format格式化Date成字符串
     */
    public static String format(Date date, String pattern) {
        return date == null ? " " : new SimpleDateFormat(pattern).format(date);
    }


    /**
     * 在日期上增加数个整月
     */
    public static Date addMonth(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, n);
        return cal.getTime();
    }

    public static String getLastDayOfMonth(String year, String month) {
        Calendar cal = Calendar.getInstance();
        // 年
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        // 月，因为Calendar里的月是从0开始，所以要-1
        // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);
        // 月份加一，得到下个月的一号
        cal.add(Calendar.MONTH, 1);
        // 下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);
        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号
    }


    public static Date addDay(Date date, int n) {

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, n);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

    public static Date getDayWithAssignTime(int hour, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, second);
        Date date = c.getTime();
        return date;
    }

    /**
     * 使用预设格式将字符串转为Date
     */
    public static Date parse(String strDate) throws ParseException {
        return StringUtils.isBlank(strDate) ? null : parse(strDate,
                getDatePattern());
    }

    /**
     * 使用参数Format将字符串转为Date
     */
    public static Date parse(String strDate, String pattern)
            throws ParseException {
        return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(
                pattern).parse(strDate);
    }

    public static Integer getAge(Date birthday) {
        Calendar calendar = new GregorianCalendar();
        Date now = new Date();
        calendar.setTime(now);
        int nowYear =calendar.get(Calendar.YEAR);
        calendar.setTime(birthday);
        int birthdayYear =calendar.get(Calendar.YEAR);

        return nowYear-birthdayYear;
    }

    public static Date addMinute(Date time, int minute) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.MINUTE, minute);
        Date date = c.getTime();
        return date;
    }

    public static Date addHour(Date time, int minute) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.HOUR, minute);
        Date date = c.getTime();
        return date;
    }

    public static int substruction(Date maxTime, Date minTime) {
        Calendar c=Calendar.getInstance();
        Calendar d=Calendar.getInstance();
        c.setTime(maxTime);
        d.setTime(minTime);
      //  long time= c.getTimeInMillis()-d.getTimeInMillis();
        long time= d.getTimeInMillis()-c.getTimeInMillis();
        int hour=new Long(time/(1000*60*60)).intValue();
        return hour;
    }

    public static long substructionTime(Date maxTime, Date minTime) {
        Calendar c=Calendar.getInstance();
        Calendar d=Calendar.getInstance();
        c.setTime(maxTime);
        d.setTime(minTime);
        //  long time= c.getTimeInMillis()-d.getTimeInMillis();
        long time= d.getTimeInMillis()-c.getTimeInMillis();
        return time;
    }


    public static String formatStringDate(String time){
        if(time == null) return null;
        return time.substring(0,10);
    }



}
